Ext.namespace('admin.role');

admin.role.Main = function(config) {
	Ext.apply(this, config);
	this.initUIComponents();
	admin.role.Main.superclass.constructor.call(this);
};

Ext.extend(admin.role.Main, Ext.Panel, {
	/**
	 * 构造函数
	 */
	initUIComponents : function() {
		
		this.recordAll = new Ext.data.Record({	
			comName : '全部',
			comCode : '-1'
		});
		
		this.westPanel = this.tree = new Ext.tree.TreePanel({
			region : "west",
			title : '角色',
			width : 245,
			border : false,
			split : true,
			collapsible : true,
			loader : new Ext.tree.TreeLoader({
				baseParams : {
					nodeId : '-1'
				},
				dataUrl : ExtJ.getActionPath('/role/queryRoleList.do')
			}),
			root : new Ext.tree.AsyncTreeNode({
				id : '-1',
				draggable : false,
				expanded : false,
				checked:true,
				text : ''
			}),
			tbar : [{
				id : 'add',
				text : '新增角色',
				iconCls : 'add',
				menu : [
                {
					id : 'add_tj',
                    text: '新增同级',
					iconCls : 'add',
					handler : this.onAddTj.createDelegate(this)
                }, {
					id : 'add_xj',
                    text: '新增下级',
					iconCls : 'add',
					handler : this.onAddXj.createDelegate(this)
                }
            ]
			},{
			id:'updateRole',
			text:'修改角色',
			iconCls : 'update',
			handler:this.onUpdateRole.createDelegate(this)
		},{
			id : 'deleteRole',
			text : '删除角色',
			iconCls : 'Delete',
			handler:this.onDeleteRole.createDelegate(this)
		}],
			autoScroll : true,
			animate : false,
			border : false,
			enableDD : false,
			containerScroll : true,
			singleExpand : false,
			draggable : false,
			rootVisible : false,
			listeners : {
				'render' : function() {
				}.createDelegate(this)
				,'beforeload': function(node, e){
					this.tree.getLoader().baseParams.nodeId = node.id;
				}.createDelegate(this)
				,'click' : this.onTreeNodeClick.createDelegate(this)
			}
		});
		this.menuRoleStore = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : ExtJ.getActionPath("role/queryRoleListAll.do")
			}),
			reader : new Ext.data.JsonReader({
				root : 'data',
				totalProperty : 'total',
				id : 'ID'
			}, [{
				name : 'comName',
				mapping : 'comName'
			}, {
				name : 'comCode',
				mapping : 'comCode'
			}])
		});
		this.menuRoleStore.load({
			callback : function(store, records) {
				this.menuRoleStore.insert(0,this.recordAll);
			}.createDelegate(this)
		});
		var menuRoleCombo = new Ext.form.MultiComboBox({
			fieldLabel : '所属权限',
			name : 'MENU_ROLE',
			id : 'MENU_ROLE',
			anchor : '90%',
			store : this.menuRoleStore,
			valueField : 'comCode',
			displayField : 'comName',
			hiddenName : 'MENU_ROLE_VALUE',
			triggerAction : 'all',
			mode : 'local',
			editable : false,
			selectOnFocus : true,
			listeners : {
				'select' : function(){
					var shareRoleIds = Ext.getCmp('MENU_ROLE').getValue();
					var ids = shareRoleIds.split(",");
					for(var i=0;i<ids.length;i++){
						if("-1"==ids[i]){
							Ext.getCmp('MENU_ROLE').checkAll();
						}
					}
				}.createDelegate(this)
			}
		});
		this.centerPanel = this.formPanel =  new Ext.form.FormPanel({
			layout : 'form',
			region : 'center',
			title : '详细信息',
			labelWidth : 125,
			labelAlign : 'right',
			autoHeight :true,
			border : false,
			frame : false,
			bodyStyle:'padding:5px 5px 0', 
			buttonAlign:'center',
			defaultType: 'textfield',
			items : [
			    new Ext.form.Hidden({
					fieldLabel : '角色ID',
					id : 'ROLE_ID',
					name : 'ROLE_ID'
				}),{
					fieldLabel : '角色名称',
					name : 'ROLE_ID',
					id : 'ROLE_ID',
					anchor : '90%',
					xtype : 'textfield'
				},{
					fieldLabel : '是否是叶子节点',
					name : 'MENU_IS_LEAF',
					id : 'MENU_IS_LEAF',
					anchor : '90%',
					readOnly : true,
					xtype : 'textfield'
				},menuRoleCombo]
		});
		
		Ext.apply(this, {
			title : '角色管理',
			closeAction : 'close',
			layout : 'border',
			border : false,
			tools : [{
						id : 'help',
						handler : function(){
							this.onHelp();				
						}.createDelegate(this)
					}],
			items : [this.westPanel,this.centerPanel],
			listeners : {
				'render' : this.initData.createDelegate(this)
			}
		})
	},
	
	initData : function(){
	},
	
	onHelp : function(){
		var dialog = new global.js.HelpWindow({key : 'xtgl.all.helprole'});
		dialog.show();
	},
	
	onTreeNodeClick : function(node, e){
		Ext.getBody().mask("数据加载中....");
		this.formPanel.getForm().reset();
		Ext.getCmp('MENU_ROLE').clearValue();
		var pageId = node.attributes.id;
		var pageName = node.attributes.text;
		var pageUrl = node.attributes.url;
		Ext.getCmp('ROLE_ID').setValue(pageId);
		Ext.getCmp('ROLE_ID').setValue(pageName);
		if(node.leaf){
			Ext.getCmp('MENU_IS_LEAF').setValue("是");
		}else{
			Ext.getCmp('MENU_IS_LEAF').setValue("否");
		}
		Ext.Ajax.request({
			url : ExtJ.getActionPath("/menu/queryMenuRoleList.do"),
			params : {
				menuId : pageId
			},
			callback : function(options, suc, response) {
				if (suc) {
					
					var text = Ext.decode(response.responseText);
					var menuRoleArr = text.data;
					var roleIds="",roleNames="";
					for(var i=0;i<menuRoleArr.length;i++){
						var roleId = menuRoleArr[i].ROLE_ID;
						var roleName = menuRoleArr[i].ROLE_NAME;
						if(0!=i){
							roleIds += ",";
							roleNames += ",";
						}
						roleIds += roleId;
						roleNames += roleName;
					}
					Ext.getCmp('MENU_ROLE').clearValue();
					Ext.getCmp('MENU_ROLE').onViewInit(roleIds);
					Ext.getCmp('MENU_ROLE').setRawValue(roleNames);
					Ext.getBody().unmask(true);
				}
			}.createDelegate(this)
		});
	},
	onSave : function(){
		var menuId = Ext.getCmp('ROLE_ID').getValue();
		var menuName = Ext.getCmp('ROLE_ID').getValue();
		var menuUrl = Ext.getCmp('MENU_URL').getValue();
		var menuRoleIds = Ext.getCmp('MENU_ROLE').getValue();
		var menuRoleNames = Ext.getCmp('MENU_ROLE').getRawValue();
		if(''==menuId){
			ExtJ.showMessage("对不起，您的操作不正确!");
			return;
		}
		if(''==menuName){
			ExtJ.showMessage("对不起，角色名称不能为空!");
			return;
		}
		this.formPanel.getForm().submit({
				method : 'post',
				url : ExtJ.getActionPath("/menu/saveMenuRole.do"),
				params : {
					menuId : menuId,
					menuName : menuName,
					menuUrl : menuUrl,
					menuRoleIds : menuRoleIds,
					menuRoleNames : menuRoleNames
				},
				reset : false,
				success : function(option, action){
					var text = Ext.decode(action.response.responseText);
					if(text.success){
						Ext.Msg.alert('提示','保存成功!');
					}else{
						Ext.Msg.alert('提示','保存失败!');
					}
				}.createDelegate(this)
		});
	},
	
	onCancel : function(){
		this.formPanel.getForm().reset();
	},
	
	onAddTj : function(){
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示","请选中一个角色节点！");
			return;
		}
		var win = new admin.role.RoleAdd({
			operType : 'tj',
			selectedTreeNode:selectNode,
			treepanel:treepanel
		});
		win.show();
	},
	
	onAddXj : function(){
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示","请选中一个角色节点！");
			return;
		}
		var win = new admin.role.RoleAdd({
			operType : 'xj',
			selectedTreeNode : selectNode,
			treepanel:treepanel
		});
		win.show();
	},
	onDeleteRole : function(){
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示","请选中一个角色节点！");
			return;
		}
		Ext.MessageBox.confirm("确认框","确定删除该角色吗？",function(data){
			if(data=="yes"){
				var id = selectNode.id;
				Ext.getBody().mask("正在删除，请稍候....");
				Ext.Ajax.request({
					url : ExtJ.getActionPath("/role/deleteRole.do"),
					params:{
						id:id
					},
					callback:function(options, suc, response){
						var text = Ext.decode(response.responseText);
						if(text.isDel){
							Ext.Msg.alert('提示','删除成功！');
						}else{
							Ext.Msg.alert('提示','删除失败！');
						}		
						treepanel.reload();   
						Ext.getBody().unmask(true);
					}.createDelegate(this)
				});
			}else{
				return;
			}
		});
	},
	onUpdateRole:function(){
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示","请选中一个角色节点！");
			return;
		}
		var win = new admin.role.RoleUpdate({
			selectedTreeNode : selectNode,
			treepanel:treepanel
		});
		win.show();
	}
});